﻿@inherits TscComponentBase
@inject IJSRuntime JSRuntime

<MHover Context="hContext">
    <div @attributes="hContext.Attrs" class="hover-pointer d-flex align-left tooltip">
        <div @onclick="() => Copy(ValueFunc(Value))" @onclick:stopPropagation>
            <SIcon Tooltip="@I18n.T("Copy value")"
                   Color="@(_copyClicked ? "success" : "gray")"
                   Class="@(hContext.Hover ? IconHoverClass : IconClass)">
                @(_copyClicked ? _checkSvg : _copySvg)
            </SIcon>
        </div>
        <MTooltip Top ContentStyle="@TooltipContentStyle" ContentClass="@TooltipContentClass">
            <ActivatorContent>
                <div @attributes="@context.Attrs" class="text-truncate @TextClass" style="@TextStyle">@TextFunc(Value)</div>
            </ActivatorContent>
            <ChildContent>
                <span>@TextFunc(Value)</span>
            </ChildContent>
        </MTooltip>
    </div>
</MHover>

@code {

    [Parameter]
    public object Value { get; set; }

    [Parameter]
    public string IconClass { get; set; } = "icon";

    [Parameter]
    public string IconHoverClass { get; set; } = "icon_hover";

    [Parameter]
    public string? TooltipContentStyle { get; set; } = "max-width:400px;word-break:break-word";

    [Parameter]
    public string? TooltipContentClass { get; set; } = "tooltip_content px-2";

    [Parameter]
    public string? TextClass { get; set; }

    [Parameter]
    public string? TextStyle { get; set; }

    [Parameter]
    public Func<object, string> TextFunc { get; set; } = obj => obj?.ToString()!;

    [Parameter]
    public Func<object, string> ValueFunc { get; set; } = obj => obj?.ToString()!;

    private bool _copyClicked = false;
    private readonly string _checkSvg = "M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z";
    private readonly string _copySvg =
        "M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z";

    private async Task Copy(string value)
    {
        _copyClicked = true;

        await JSRuntime.InvokeVoidAsync(JsInteropConstants.Copy, value);

        await Task.Delay(500);

        _copyClicked = false;
    }
}